home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C++ / Frameworks / Sprocket Framework DR2 / Sprocket Framework / SerialNumberProtection.cp < prev    next >
Text File  |  1996-06-15  |  6KB  |  292 lines

  1. /*
  2.  
  3.     File:        SerialNumberProtection.cp
  4.     Project:    Sprocket Framework 1.1 (DR2), released 6/15/96
  5.     Contains:    Class which implements serial number-based protection (ie only X copies allowed
  6.                 to being running at any one time)
  7.     To Do:        ?
  8.  
  9.     Sprocket Major Contributors:
  10.     ----------------------------
  11.     Dave Falkenburg, producer of Sprocket 1.0
  12.     Bill Hayden,     producer of Sprocket 1.1
  13.     Steve Sisak,     producer of the upcoming Sprocket 2.0
  14.     
  15.     Pete Alexander        Steve Falkenburg    Randy Thelen
  16.     Eric Berdahl        Nitin Ganatra        Chris K. Thomas
  17.     Marshall Clow        Dave Hershey        Leonard Rosenthal
  18.     Tim Craycroft        Dave Mark            Dean Yu
  19.     David denBoer        Gary Powell
  20.     Cameron Esfahani    Jon Summers            Apple Computer, Inc.
  21.         
  22.     Comments, Additions, or Corrections:
  23.     ------------------------------------
  24.     Bill Hayden, Nikol Software <nikol@codewell.com>
  25.  
  26. */
  27.  
  28.  
  29. #include "SerialNumberProtection.h"
  30. #include "SprocketMacros.h"
  31.  
  32. #include "UString.h"
  33.  
  34. // if set to zero, Sprocket does not bother to check for duplicate serial numbers over the network
  35.  
  36. // • Developer Note:
  37. // -----------------
  38. // While Network Security does work, it sometimes has strange, mostly harmless side-effects
  39. // such as making the TE carat blink extremely fast.  Use at your own risk and report bugs
  40. // to me please at <nikol@codewell.com>.
  41.  
  42. #ifndef qNetworkSecurity
  43. #define qNetworkSecurity    0
  44. #endif
  45.  
  46.  
  47. #if qNetworkSecurity
  48.  
  49. #include <Devices.h>
  50. const Str32     kSS = "\pSprocketSecurity";
  51.  
  52. #endif
  53.  
  54.  
  55. /*****************************************************************************/
  56.  
  57.  
  58.  
  59. TSerialNumberProtection::TSerialNumberProtection(short    ResourceID)
  60. {
  61.     fResourceID = ResourceID;
  62.     fNumOfLicenses = kNoLicense;
  63.     
  64.     try
  65.         {
  66.         Handle temp;
  67.         
  68.         temp = Get1Resource('STR#', ResourceID);
  69.         FailResErrorOrNil(temp);
  70.         ReleaseResource(temp);
  71.         FailResError();
  72.         }
  73.     catch(OSErr err)
  74.         {
  75.         throw err;    // rethrow it to next level
  76.         }
  77. }
  78.  
  79.  
  80.  
  81. /*****************************************************************************/
  82.  
  83. // This must be called AFTER either ReadRegistration() or WriteRegistration()
  84.  
  85. OSErr    TSerialNumberProtection::EnableProtection(void)
  86. {
  87. #if qNetworkSecurity
  88.     OSErr            err;
  89.     ATPParamBlock    thePB;
  90.     short            MPPDriverRef;
  91.     
  92.     #define alert_Error            129
  93.     
  94.     // open the .MPP driver
  95.     
  96.     err = OpenDriver( "\p.MPP", &MPPDriverRef );
  97.     if ( noErr != err )
  98.         return err;
  99.  
  100.     // open socket
  101.     
  102.     SetMem( &thePB, 0, sizeof( thePB ) );
  103.     err = POpenATPSkt(&thePB, false);
  104.     
  105.     if( noErr != err )
  106.         return err;
  107.  
  108.     // register via nbp
  109.     
  110.     err = RegisterName(thePB.ATP.atpSocket);
  111.         
  112.     return err;
  113. #else
  114.     return noErr;
  115. #endif
  116. }
  117.  
  118.  
  119.  
  120. /*****************************************************************************/
  121.  
  122.  
  123.  
  124.  
  125. OSErr TSerialNumberProtection::RegisterName( UInt8 inSocket )
  126. {
  127. #if qNetworkSecurity
  128.     MPPParamBlock    pb;
  129.  
  130.  
  131.     NBPSetNTE( (Ptr)&fNTE, (ConstStr32Param)&fSerialNumber, (ConstStr32Param)kSS, "\p*", inSocket );
  132.  
  133.     pb.NBPinterval = 2;
  134.     pb.NBPcount = 3;
  135.     pb.NBPentityPtr = (Ptr)&fNTE;
  136.     pb.NBPverifyFlag = 1;
  137.     
  138.     return( PRegisterName( &pb,false ) );
  139. #else
  140.  
  141.     #pragma unused (inSocket)
  142.  
  143.     return noErr;
  144.     
  145. #endif
  146. }
  147.  
  148.  
  149. /*****************************************************************************/
  150.  
  151.  
  152.  
  153. TSerialNumberProtection::~TSerialNumberProtection()
  154. {
  155. #if qNetworkSecurity
  156.     MPPParamBlock pb;
  157.  
  158.  
  159.     pb.NBPentityPtr = (Ptr)&fNTE.nt.entityData;
  160.     PRemoveName(&pb, false);
  161. #endif
  162. }
  163.  
  164.  
  165.  
  166. /*****************************************************************************/
  167.  
  168. // overload this function to get the information from a different place
  169.  
  170. OSErr TSerialNumberProtection::ReadRegistration(StringPtr SN, StringPtr Name, StringPtr Company)
  171. {
  172.     try
  173.         {
  174.         GetIndString(fSerialNumber, fResourceID, 1);
  175.         FailResError();
  176.         
  177.         GetIndString(fName, fResourceID, 2);
  178.         FailResError();
  179.  
  180.         GetIndString(fCompany, fResourceID, 3);
  181.         FailResError();
  182.  
  183.         GetIndString(fPlainName, fResourceID, 4);
  184.         FailResError();
  185.         }
  186.     catch(OSErr err)
  187.         {
  188.         return err;
  189.         }
  190.  
  191.     UnmangleRegistration();
  192.     
  193.     pcpy(SN, fSerialNumber);            // keep a safe copy
  194.     pcpy(Name, fName);
  195.     pcpy(Company, fCompany);
  196.     
  197.     return noErr;
  198. }
  199.  
  200.  
  201.  
  202. /*****************************************************************************/
  203.  
  204. // overload this function to put the information in a different place
  205.  
  206. OSErr TSerialNumberProtection::WriteRegistration(StringPtr SN, StringPtr Name, StringPtr Company)
  207. {
  208.     OSErr    err;
  209.     
  210.     pcpy(fSerialNumber, SN);            // keep a safe copy
  211.     pcpy(fName, Name);
  212.     pcpy(fCompany, Company);
  213.     pcpy(fPlainName, Name);
  214.     
  215.     MangleRegistration();
  216.     
  217.     err = SetIndString(fSerialNumber, fResourceID, 1);
  218.     if (noErr != err)
  219.         return err;
  220.  
  221.     err = SetIndString(fName, fResourceID, 2);
  222.     if (noErr != err)
  223.         return err;
  224.  
  225.     err = SetIndString(fCompany, fResourceID, 3);
  226.     if (noErr != err)
  227.         return err;
  228.     
  229.     err = SetIndString(Name, fResourceID, 4);
  230.     return err;
  231. }
  232.  
  233.  
  234.  
  235. /*****************************************************************************/
  236.  
  237. // overload this function to provide verification that the SN that was entered correctly
  238.  
  239. OSErr TSerialNumberProtection::VerifyRegistration(StringPtr /* SN */, StringPtr /* Name */, StringPtr /* Company */)
  240. {
  241.     return noErr;
  242. }
  243.  
  244.  
  245.  
  246. /*****************************************************************************/
  247.  
  248. // overload this function to provide your own enciphering mechanism
  249.  
  250. void TSerialNumberProtection::MangleRegistration(void)
  251. {
  252.     for (short x = 1; x < fName[0]; x++)
  253.         {
  254.         fName[x] += (fCompany[(x % (fCompany[0] - 1)) + 1] % 9) - 5;
  255.         }
  256.  
  257.     for (short x = 1; x < fCompany[0]; x++)
  258.         {
  259.         fCompany[x] += (fSerialNumber[(x % (fSerialNumber[0] - 1)) + 1] % 9) - 5;
  260.         }
  261.  
  262.     for (short x = 1; x < fSerialNumber[0]; x++)
  263.         {
  264.         fSerialNumber[x] += (fPlainName[(x % (fPlainName[0] - 1)) + 1] % 9) - 5;
  265.         }
  266. }
  267.  
  268.  
  269. /*****************************************************************************/
  270.  
  271. // overload this function to provide your own deciphering mechanism
  272.  
  273. void TSerialNumberProtection::UnmangleRegistration(void)
  274. {
  275.     for (short x = 1; x < fSerialNumber[0]; x++)
  276.         {
  277.         fSerialNumber[x] -= (fPlainName[(x % (fPlainName[0] - 1)) + 1] % 9) - 5;
  278.         }
  279.         
  280.     for (short x = 1; x < fCompany[0]; x++)
  281.         {
  282.         fCompany[x] -= (fSerialNumber[(x % (fSerialNumber[0] - 1)) + 1] % 9) - 5;
  283.         }
  284.  
  285.     for (short x = 1; x < fName[0]; x++)
  286.         {
  287.         fName[x] -= (fCompany[(x % (fCompany[0] - 1)) + 1] % 9) - 5;
  288.         }
  289. }
  290.  
  291.  
  292.